package com.baogu.database.controller;

import com.alibaba.druid.util.StringUtils;
import com.baogu.database.service.SensorDataImportService;
import com.baogu.database.service.SensorWarningImportService;
import com.baogu.database.service.SensorWarningService;
import com.baogu.model.common.HttpResult;
import com.baogu.model.database.SensorData;
import com.baogu.model.database.SensorWarning;
import com.baogu.model.database.SensorWarningImport;
import com.baogu.model.database.where.SensorWarningImportWhere;
import com.baogu.model.database.where.SensorWarningWhere;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sensorWarning")
public class SensorWarningController {

    @Autowired
    SensorWarningService sensorWarningService;
    @Autowired
    SensorWarningImportService sensorWarningImportService;

    @GetMapping("/getNewSensorWarning")
    public HttpResult getNewSensorWarning(){
        SensorWarningImportWhere where = new SensorWarningImportWhere();
        where.setImportState(0);
        List<SensorWarningImport> list = sensorWarningImportService.allList(where);
        return HttpResult.OK(list);
    }

    @GetMapping("/completeImpSensorWarning/{vsion}")
    public HttpResult completeImpSensorWarning(@PathVariable("vsion") String vsion){
        if(StringUtils.isEmpty(vsion)){
            return HttpResult.ERROR("1100","参数错误");
        }
        SensorWarningImportWhere where = new SensorWarningImportWhere();
        where.setVersion(vsion);
        where.setImportState(1);
        Integer result = sensorWarningImportService.updataDataImport(where);
        if(result > 0){
            return HttpResult.OK(null);
        }else{
            HttpResult httpResult = new HttpResult();
            httpResult.setMsg("10001");
            httpResult.setMsg("update fail");
            return httpResult;
        }
    }

    @GetMapping("/getSensorSensorWarningByVsion/{vsion}/{tableDate}")
    public HttpResult getSensorSensorWarningByVsion(@PathVariable("vsion") String vsion,@PathVariable("tableDate") String tableDate){
        if(StringUtils.isEmpty(tableDate)){
            return HttpResult.ERROR("1100","参数错误");
        }
        if(StringUtils.isEmpty(vsion)){
            return HttpResult.ERROR("1100","参数错误");
        }
        SensorWarningWhere where = new SensorWarningWhere();
        where.setVersion(vsion);
        where.setTableDate(tableDate);
        List<SensorWarning> list = sensorWarningService.allList(where);
        return HttpResult.OK(list);
    }
}
